package org.jsola.contract.web;

import org.jsola.contract.constant.ContractConstants;
import org.jsola.contract.dto.*;
import org.jsola.contract.entity.ContractDO;
import org.jsola.contract.service.IContractService;
import org.jsola.contract.service.ISignService;
import org.jsola.contract.vo.ContractVO;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author JanusMix
 */
@RestController("contractSignController")
@RequestMapping(ContractConstants.API_PATH + "/v1/signs")
public class SignController implements ISignControllerAPI {

    @Autowired
    private ISignService signService;

    @Override
    @PostMapping("/publishTemplateSign")
    public Result<List<ContractVO>> publishTemplateSign(@Valid @RequestBody PublishSignDTO publishSignDTO) {
        TokenUser tokenUser = UserContext.getUser();
        List<ContractVO> contractVOList = signService.publishTemplateSign(publishSignDTO, tokenUser);
        return Result.success(contractVOList);
    }

    @Autowired
    private IContractService contractService;

    @PostMapping("/modifyContract")
    public Result<List<String>> modifyContract(String subjectId, Long sealId) {

        TokenUser tokenUser = UserContext.getUser();
        List<ContractDO> contractDOList = contractService.selectBySubjectId(subjectId);
        ParamException.notEmpty(contractDOList, "无合同");
        return Result.success(
                signService.modifyContract(contractDOList, sealId, tokenUser)
        );
    }

    @Override
    @PostMapping("/personSign")
    public Result<String> personSign(@Valid @RequestBody PersonSignDTO personSignDTO) {
        TokenUser tokenUser = UserContext.getUser();
        signService.personSign(personSignDTO.getContractId(), personSignDTO.getSealString()
                , personSignDTO.getCode(), Boolean.TRUE, tokenUser);
        return Result.success();
    }

    @Override
    @PostMapping("/enterpriseSign")
    public Result<String> enterpriseSign(@Valid @RequestBody EnterpriseSignDTO enterpriseSignDTO) {
        TokenUser tokenUser = UserContext.getUser();
        signService.enterpriseSign(enterpriseSignDTO.getCompanyId(), enterpriseSignDTO.getContractId(), tokenUser);
        return Result.success();
    }

    @Override
    @PostMapping("/enterpriseSignAgain")
    public Result<String> enterpriseSignAgain(@Valid @RequestBody EnterpriseSignDTO enterpriseSignDTO) {
        TokenUser tokenUser = UserContext.getUser();
        signService.contractSignAgain(enterpriseSignDTO.getContractId(), enterpriseSignDTO.getCompanyId(), tokenUser);
        return Result.success();
    }

    @Override
    @PostMapping("/enterpriseSignAgainBatch")
    public Result<String> enterpriseSignAgainBatch(@Valid @RequestBody EnterpriseSignDTO enterpriseSignDTO) {
        TokenUser tokenUser = UserContext.getUser();
        List<Long> contractIds = enterpriseSignDTO.getContractIds();
        if (CollectionUtils.isEmpty(contractIds)) {
            throw new ParamException("请选择需要公司确认签署的合同");
        }
        for (Long contractId : contractIds) {
            signService.contractSignAgainAsync(contractId, enterpriseSignDTO.getCompanyId(), tokenUser);
        }
        return Result.success();
    }

    @Override
    @PostMapping("/sendCode")
    public Result<Boolean> sendCode() {
        TokenUser tokenUser = UserContext.getUser();
        return signService.sendCode(tokenUser);
    }

    @Override
    @PostMapping("/wordSignCompany")
    public Result<String> wordSignCompany(@Valid @RequestBody PublishWordCompanySignDTO publishWordCompanySignDTO) {
        TokenUser tokenUser = UserContext.getUser();

        signService.wordSignCompany(publishWordCompanySignDTO, tokenUser);
        return Result.success();
    }

    @Override
    @PostMapping("/wordSignPeople")
    public Result<String> wordSignPeople(@Valid @RequestBody PublishWordPeopleSignDTO publishWordPeopleSignDTO) {
        TokenUser tokenUser = UserContext.getUser();

        signService.wordSignPeople(publishWordPeopleSignDTO, tokenUser);
        return Result.success();
    }
}
